package design.pattern.TemplateMethod;

public abstract class  Account {

    public static void main(String[] args) {
        Account account = new MoneyMarketAccount();
        System.out.println("活期账号的利息数额为：" + account.calculateInterest());
         account = new CDAccount();
        System.out.println("定期账号的利息数额为：" + account.calculateInterest());
    }

    /**
     * 模板方法，计算利息数额
     * @return    返回利息数额
     */
    public final double calculateInterest(){
        //得到利率
        double interestRate = doCalculateInterestRate();
        //得到存储类型
        String accountType = doCalculateAccountType();
        //跟进存储类型  得到本金
        double amount = calculateAmount(accountType);
        //本金*利率
        return amount * interestRate;

    }

    /**
     * 基本方法留给子类实现 获取存储类型
     */
    protected abstract String doCalculateAccountType();
    /**
     * 基本方法留给子类实现 获取利率
     */
    protected abstract double doCalculateInterestRate();
    /**
     * 基本方法，已经实现 跟进存储类型得到 本金
     */
    private double calculateAmount(String accountType){
        /**
         * 省略相关的业务逻辑
         */
        return 7243.00;
    }
}

/**
 * 活期  求 利息
 */
class MoneyMarketAccount extends Account {
    @Override
    protected String doCalculateAccountType() {

        return "Money Market";
    }
    @Override
    protected double doCalculateInterestRate() {
        return 0.045;
    }
}

/**
 * 定期   求  利息
 */
 class CDAccount extends Account {
    @Override
    protected String doCalculateAccountType() {
        return "Certificate of Deposite";
    }
    @Override
    protected double doCalculateInterestRate() {
        return 0.06;
    }
}

    
